package com.tongji.showroom.service
{ 
	import alternativa.types.Long;
	
	import com.tongji.showroom.events.AddUserProductEvent;
	import com.tongji.showroom.model.UserModel;
	import com.tongji.showroom.vo.Model;
	
	import flash.system.Security;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.soap.WebService;
	
	import org.robotlegs.mvcs.Actor;
	
	/**
	 * 
	 * @author jiarong
	 * 
	 */
	public class UserProductService extends Actor
	{
		public var ws:WebService;
		private var deleteId:Number = 0;
		[Inject]
		public var model:UserModel;
		
		public function UserProductService()
		{
			super();
			ws = new WebService;
			ws.wsdl = "http://share.rryjw.com/VRService.asmx?wsdl";
			ws.useProxy = false;
			ws.loadWSDL();
			Security.loadPolicyFile("http://img.rryjw.com/crossdomain.xml");
		}	
		
		/**
		 *用户添加一个产品，实际就是添加一张图片 
		 * @param userID
		 * @param imgUrlList
		 * 
		 */		
		public function addUserProduct(userId:Number, imgList:Array):void
		{
			ws.AddUserImgList(userId, imgList);
			ws.AddUserImgList.addEventListener(ResultEvent.RESULT, addUserProductResult);
			ws.AddUserImgList.addEventListener(FaultEvent.FAULT, handleFault);	
		}
		
		/**
		 *根据用户ID，获取当前用户的商品信息 
		 * @author jiarong
		 * @param userID
		 * 
		 */		
		public function getUserProduct(userID:Number):void
		{
			ws.GetUserProductByUid(userID);
			ws.GetUserProductByUid.addEventListener(ResultEvent.RESULT, getUserProductResult);
			ws.GetUserProductByUid.addEventListener(FaultEvent.FAULT, handleFault);		
		}
		
		/**
		 *根据商品id，删除该商品 
		 * @author jiarong
		 * @param imgId
		 */		
		public function deleteProductById(imgId:Number):void
		{
			ws.DeleteProduct(imgId);
			deleteId = imgId;
			ws.DeleteProduct.addEventListener(ResultEvent.RESULT, deleteProductResult);
			ws.DeleteProduct.addEventListener(FaultEvent.FAULT, handleFault);		
		}

		//陈中原
		private function addUserProductResult(event:ResultEvent):void
		{	
			dispatch(new AddUserProductEvent(AddUserProductEvent.UPDATE_USER_MODEL, model.userID, null, 0, event.result as String));
			
			/*			dispatch(new AddUserProductEvent(AddUserProductEvent.GET_USER_PRODUCT, 22));
		*/		}
		
		private function getUserProductResult(event:ResultEvent):void
		{
			dispatch(new AddUserProductEvent(AddUserProductEvent.UPDATE_USER_MODEL, model.userID, null, 0, event.result as String));
		}
		
		private function deleteProductResult(event:ResultEvent):void
		{
			if (event.result)
				dispatch(new AddUserProductEvent(AddUserProductEvent.DELETE_A_PICTURE, model.userID, null, deleteId));
			else
				Alert.show("对不起，您的商品未删除");
		}
		
		private function handleFault(event:FaultEvent):void
		{
			Alert.show("失败" + event.fault.toString());
		}
	}
}